﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Projektet.Model
{
    public enum PlayerType
    {
        Human,
        Npc
    }

    abstract class Player
    {
        public PlayerType _playerType;
        public Vector2 _position;
        public Vector2 _spawnPosition;
        public UnitStats _unitStats = new UnitStats();
        public List<CombatUnit> _unitList = new List<CombatUnit>();
        private List<CombatUnit> _unitsToSpawn = new List<CombatUnit>();
        public Castle _castle;

        protected int _levelCount;

        public float _gold;
        public float _goldIncome;
        public const float _spawnCd = 500f;
        protected float _timeSinceSpawn;
        public float _pikeCost = 75f;
        public float _knightCost = 100f;
        public float _archerCost = 50f;

        public void Update(GameTime gameTime)
        {
            //Is called in GameModel since the player and npc requires different data
            //SpecificUpdate(gameTime);
            _gold += (float)gameTime.ElapsedGameTime.TotalSeconds * _goldIncome;
            _timeSinceSpawn += gameTime.ElapsedGameTime.Milliseconds;
            if(_unitsToSpawn.Count > 0 && _timeSinceSpawn > _spawnCd)
            {
                _unitList.Add(_unitsToSpawn[0]);
                _unitsToSpawn.RemoveAt(0);
                _timeSinceSpawn = 0f;
            }

            UpdateUnits(gameTime);
        }

        public virtual void SpecificUpdate(GameTime gameTime) { }

        public void UpdateUnits(GameTime gameTime)
        {
            foreach (CombatUnit cu in _unitList)
            {
                cu.Update(gameTime);
            }
        }

        public void AddPikeman() 
        {
            if (_gold > _pikeCost)
            {
                _unitsToSpawn.Add(new Pikeman(_playerType, new Vector2(_spawnPosition.X, _spawnPosition.Y), _unitStats));
                _gold -= _pikeCost;
            }
        }

        public void AddKnight()
        {
            if (_gold > _knightCost)
            {
                _unitsToSpawn.Add(new Knight(_playerType, new Vector2(_spawnPosition.X, _spawnPosition.Y), _unitStats));
                _gold -= _knightCost;
            }
        }

        public void AddArcher()
        {
            if (_gold > _archerCost) 
            {
                _unitsToSpawn.Add(new Archer(_playerType, new Vector2(_spawnPosition.X, _spawnPosition.Y), _unitStats));
                _gold -= _archerCost;
            }
        }
    }
}
